********************************************************************************
* Project: Measuring party system nationalisation
*  Author: Kazuma Mizukoshi
*    Date: 27 August 2020
********************************************************************************

********************************************************************************
* TASK-1: Import a dataset
* TASK-2: Estimate PSNS
* TASK-3: Estimate national, district, and election components by the 3-Lv. LMM
********************************************************************************

********************************************************************************
* TASK-1: Import a dataset
********************************************************************************
use "T-Dataset.xlsx"

********************************************************************************
* TASK-2: Estimate PSNS
********************************************************************************
*(egen_inequal is a user-written program for download)
	replace VoteShare = VoteShare+.0001 if VoteShare == 0
	egen Gini = inequal(VoteShare), by(Year Party)
	gen PNS = 1-Gini
	collapse VoteShare, by(Country Year Party PNS)
	gen PNSw = VoteShare/100 * PNS
	collapse (sum) PNSw, by(Country Year)

********************************************************************************
* TASK-3: Estimate national, district, and election components by the 3-Lv. LMM
********************************************************************************
* Models for Country A
*Model 1
	xtmixed VoteShare Year if Country =="A", mle var
    est store m1
    estat ic
	
* Model 2
	xtmixed VoteShare Year ||Party:Year if Country == "A", mle var cov(unstr) 
    est store m2
    estat ic
	
* Model 3 (Reported by Table 3)
	xtmixed VoteShare Year ||Party:Year  ||District:Year if Country == "A", mle var cov(unstr) 
    est store m3
    estat ic
	
* Models for Country B
*Model 1
	xtmixed VoteShare Year if Country =="B", mle var
    est store m1
    estat ic
	
* Model 2
	xtmixed VoteShare Year ||Party:Year if Country == "B", mle var cov(unstr) 
    est store m2
    estat ic
	
* Model 3 (Reported by Table 3)
	xtmixed VoteShare Year ||Party:  ||District: if Country == "B", mle var cov(unstr)
    est store m3
    estat ic



